﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Farigola.Engine.SystemData;
using Farigola.Engine.SystemData.Entity.Type;
using Farigola.Engine.SystemData.Entity;

namespace Farigola.Engine.Factories
{
    public class FactoryCommon
    {
        //=====================================================================
        #region Data members.

        /// <summary>
        /// The unique errors manager of the engine application.
        /// Provided to main object in the engine (workspace, project,...).
        /// </summary>
        //private ErrorMgr _errorMgr;
        /// <summary>
        /// Manager of tasks activity in the application (trace)
        /// Manage alos errors occurs in the engine.
        /// </summary>
        private AppActivityMgr _appActivityMgr;

        /// <summary>
        /// The unique system data mgr of the engine workspace of the core.
        /// </summary>
        private SystemDataMgr _systemDataMgr;

        /// <summary>
        /// The unique worskpace of the engine core.
        /// Is referenced here to be sure to create it only one time.
        /// </summary>
        private Workspace _workspace;

        #endregion

        //=====================================================================
        #region Constructors.

        //---------------------------------------------------------------------
        /**
         * Constructor.
         */
        public FactoryCommon(AppActivityMgr appActivityMgr)
        {
            _appActivityMgr = appActivityMgr;
        }

        #endregion

        //=====================================================================
        #region get/set methods.

        //---------------------------------------------------------------------
        public void SetAppActivityMgr(AppActivityMgr appActivityMgr)
        {
            _appActivityMgr = appActivityMgr;
        }

        //---------------------------------------------------------------------
        public SystemDataMgr GetSystemDataMgr()
        {
            return _systemDataMgr;
        }


        //---------------------------------------------------------------------
        /// <summary>
        /// Return the EntityId representing the EntTypeFolder.
        /// </summary>
        /// <returns></returns>
        public EntityId GetEntTypeFolderEntityId()
        {
            return _systemDataMgr.GetEntTypeFolderEntityId();
        }

        //---------------------------------------------------------------------
        /// <summary>
        /// Return the entity type, by the id.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public EntTypeBase GetTypeById(EntityId id)
        {
            return _systemDataMgr.FindEntTypeById(id);
        }

        #endregion

        //=====================================================================
        #region Is methods.

        //---------------------------------------------------------------------
        /// <summary>
        /// Return true if the entity type is an entity type folder.
        /// </summary>
        /// <param name="entType"></param>
        /// <returns></returns>
        public bool IsEntTypeFolder(EntTypeBase entType)
        {
            return _systemDataMgr.IsEntTypeFolder(entType);
        }

        //---------------------------------------------------------------------
        /// <summary>
        /// Return true if the entity type is an entity type basic (string, int, ...).
        /// </summary>
        /// <param name="entType"></param>
        /// <returns></returns>
        public bool IsEntTypeBasic(EntTypeBase entType)
        {
            return _systemDataMgr.IsEntTypeBasic(entType);
        }

        //---------------------------------------------------------------------
        /// <summary>
        /// Return true if the entity type is an entity type string.
        /// </summary>
        /// <param name="entType"></param>
        /// <returns></returns>
        public bool IsEntTypeString(EntTypeBase entType)
        {
            return _systemDataMgr.IsEntTypeString(entType);
        }

        //---------------------------------------------------------------------
        /// <summary>
        /// Return true if the entity type is an entity type class base.
        /// </summary>
        /// <param name="entType"></param>
        /// <returns></returns>
        public bool IsEntTypeClassBase(EntTypeBase entType)
        {
            return _systemDataMgr.IsEntTypeClassBase(entType);
        }

        //---------------------------------------------------------------------
        /// <summary>
        /// Return true if the entity type is an entity type class (std).
        /// </summary>
        /// <param name="entType"></param>
        /// <returns></returns>
        public bool IsEntTypeClass(EntTypeBase entType)
        {
            return _systemDataMgr.IsEntTypeClass(entType);
        }

        //---------------------------------------------------------------------
        /// <summary>
        /// Return true if the entity type is an entity type class anonym.
        /// </summary>
        /// <param name="entType"></param>
        /// <returns></returns>
        public bool IsEntTypeClassAnonym(EntTypeBase entType)
        {
            return _systemDataMgr.IsEntTypeClassAnonym(entType);
        }

        #endregion

        //=====================================================================
        // Check methods.

        //---------------------------------------------------------------------
        /**
         * Check the name, do a general/common control.
         * Must be not null and not blank/empty.
         * 
         * Its a general method, used for Project, Entity,...
         * 
         * @param name
         * @return
         */
        public bool checkSyntaxName(String name)
        {
            if (name == null)
                return false;

            if (name.Trim().Length==0)
                return false;

            return true;
        }

        //---------------------------------------------------------------------
        /**
         * check the syntax of the name of the project.
         * @param name
         */
        public bool checkSyntaxNameProject(String name)
        {
            if (!checkSyntaxName(name))
                return false;

            return true;
        }

        //---------------------------------------------------------------------
        public bool checkSyntaxNameTypeClassDataMember(String name)
        {
            if (!checkSyntaxName(name))
                return false;

            return true;
        }

        //---------------------------------------------------------------------
        /**
         * check the syntax of the name of the user.
         * @param name
         */
        public bool checkSyntaxNameUser(String name)
        {
            if (!checkSyntaxName(name))
                return false;

            return true;
        }

        //=====================================================================
        #region EntityId methods.

        //---------------------------------------------------------------------
        /// <summary>
        /// Generate a new entity Id.
        /// </summary>
        /// <returns></returns>
        public EntityId CreateEntityId()
        {
            EntityId entId = new EntityId(true);

            return entId;
        }

        //---------------------------------------------------------------------
        /// <summary>
        /// Build/generate a new Id, based on a string id format.
        /// if wrong, return null.
        /// </summary>
        /// <param name="idStr"></param>
        /// <returns></returns>
        public EntityId CreateEntityId(String idStr)
        {
            EntityId entId = new EntityId();

            bool res = entId.SetFromIdStr(idStr);

            if (!res)
                return null;

            // ok, the id str is right
            return entId;
        }


        //---------------------------------------------------------------------
        /**
         * check the id, if null or empty, generate a new one.
         * @param id
         * @return
         */
        /*
        public EntityId getNewIdIfNullOrEmpty(EntityId id)
        {
            try
            {
                if (id == null)
                    return new EntityId();
	
                // todo: a faire
                //if (id.Equals(Guid.Empty))
                //    return new EntityId();

                // the id is ok, return it
                return id;
            }
            catch(Exception e)
            {
                return new EntityId();
            }
        }
        */

        #endregion


        //=====================================================================
        #region Create SystemDataMgr and workspace methods.

        //---------------------------------------------------------------------
        /// <summary>
        /// Create the (unique) system data manager.
        /// </summary>
        /// <param name="factory"></param>
        /// <returns></returns>
        public SystemDataMgr CreateSystemDataMgr(Factory factory)
        {
            if (_systemDataMgr != null)
                // already exists
                return null;

            // Instantiate and provide the factory
            _systemDataMgr = new SystemDataMgr(factory, _appActivityMgr);


            return _systemDataMgr;
        }

        //---------------------------------------------------------------------
        public TypeMgr CreateTypeMgr()
        {
            return new TypeMgr(_appActivityMgr);
        }

        //---------------------------------------------------------------------
        /// <summary>
        /// create the (unique) workspace of the engine core.
        /// </summary>
        /// <param name="factory"></param>
        /// <returns></returns>
        public Workspace CreateWorkspace(Factory factory)
        {
            if (_workspace != null)
                // already exists
                return null;

            // Instantiate and provide the factory
            _workspace = new Workspace(factory, _appActivityMgr);

            return _workspace;
        }

        //---------------------------------------------------------------------
        /// <summary>
        /// create the entity finder of the project.
        /// </summary>
        /// <param name="systemDataMgr"></param>
        /// <param name="typeMgr"></param>
        /// <returns></returns>
        public EntityFinder CreateEntityFinder(SystemDataMgr systemDataMgr, TypeMgr typeMgr)
        {
            return new EntityFinder(systemDataMgr, typeMgr);
        }


        #endregion
    }
}
